package com.spm.web;

import com.spm.pojo.Student;
import com.spm.pojo.Grade;
import com.spm.pojo.StudentGrade;
import com.spm.pojo.Teacher;
import com.spm.service.GradeService;
import com.spm.service.impl.GradeServiceImpl;
import com.spm.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "GradeServlet", value = "/GradeServlet")
public class GradeServlet extends BaseServlet {

    private SqlSession sqlSession = MybatisUtils.getSqlSession();

    private GradeService gradeService = new GradeServiceImpl(sqlSession);

    /**
     * 说明：
     * 学生查询自己所有课程的成绩，可以从返回参数gradeList获取
     * 课程号、课程名、任课老师等参数
     * （具体看Grade类）
     *
     * @throws ServletException
     * @throws IOException
     * @url /GradeServlet?action=studentQuery
     * <p>
     * ---需要获取的参数：
     * 1、从session中获取student类
     * 2、从student类中获取学号no
     * <p>
     * ---返回的参数（保存在request中）：
     * 类型：List<Grade>
     * 参数名：gradeList
     */
    protected void studentQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取session中保存的学生成绩类
        HttpSession session = req.getSession();
        Student student = (Student) session.getAttribute("student");
        //从学生成绩类中获取学生编号
        String no = student.getNo();

        //2、调用gradeService获取成绩列表
        List<Grade> gradeList = gradeService.queryForStudent(no);
        sqlSession.close();

        //3、保存gradeList到Request域中
        req.setAttribute("gradeList", gradeList);

        //4、请求转发到...页面
        req.getRequestDispatcher("").forward(req, resp);

    }

    /*** 说明：
     * 老师查询自己某门课程所有学生的成绩，可以从返回参数获得
     *学生编号、学生姓名、班级等参数
     * （具体看StudentGrade类）
     *
     * @url /GradeServlet?action=teacherQueryAll
     *
     * ---需要获取的参数：
     * 1、从session中获取Teacher类
     * 2、从Teacher类中获取编号tid
     * 3、从request中获取课程号cno
     *
     * ---返回的参数（保存在request中）：
     * 类型：List<StudentGrade>
     * 参数名：gradesOfCourse
     *
     * @throws ServletException
     * @throws IOException
     */
    protected void teacherQueryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取session中保存的教师类
        HttpSession session = req.getSession();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        //从教师类中获取教师编号
        String tid = teacher.getTid();

        //2、从request中获取cno参数
        String cno = req.getParameter("cno");

        //3、调用gradeService获取成绩列表
        List<StudentGrade> studentGrades = gradeService.queryByCourse(tid, cno);
        sqlSession.close();

        //4、保存studentGrades至request域中
        req.setAttribute("gradesOfCourse", studentGrades);

        for (StudentGrade s :
                studentGrades) {
            System.out.println(s);
        }

        //5、请求转发到...页面
        req.getRequestDispatcher("").forward(req, resp);

    }

    /**
     * 说明：老师查询自己某门课程某位学生的成绩
     *
     * @throws ServletException
     * @throws IOException
     * @url //GradeServlet?action=teacherQueryOne
     * <p>
     * ---需要获取的参数：
     * 1、从session中获取Teacher类
     * 2、从Teacher类中获取编号tid
     * 3、从request中获取课程号cno
     * 4、从request中获取学生学号sid
     * <p>
     * ---返回的参数（保存在request中）：
     * 类型：StudentGrade
     * 参数名：gradeOfOne
     */
    protected void teacherQueryOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取session中保存的教师类
        HttpSession session = req.getSession();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        //从教师类中获取教师编号
        String tid = teacher.getTid();

        //2、从request中获取cno参数和sid参数
        String cno = req.getParameter("cno");
        String sid = req.getParameter("sid");

        //3、调用gradeService获取成绩
        StudentGrade studentGrade = gradeService.queryByCAS(tid, cno, sid);
        sqlSession.close();

        //4、保存studentGrades至request域中
        req.setAttribute("gradeOfOne", studentGrade);

        //5、请求转发到...页面
        req.getRequestDispatcher("").forward(req, resp);

    }

    protected void calculateGrade(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }

    /**
     * 说明：筛选不及格成绩
     *
     * @url /GradeServlet?action=screenFail
     *
     * ---需要获取的参数：
     * 1、从session中获取Teacher类
     * 2、从Teacher类中获取编号tid
     * 3、从request中获取课程号cno
     *
     * ---返回的参数（保存在request中）：
     * 类型：List<StudentGrade>
     * 参数名：failExam
     *
     *  @throws ServletException
     *  @throws IOException
     */
    protected void screenFail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取session中保存的教师类
        HttpSession session = req.getSession();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        //从教师类中获取教师编号
        String tid = teacher.getTid();

        //2、从request中获取cno参数
        String  cno = req.getParameter("cno");

        //3、调用gradeService获取成绩
        List<StudentGrade> failExam = gradeService.failScreen(tid, cno);
        sqlSession.close();

        //4、保存studentGrades至request域中
        req.setAttribute("failExam", failExam);

        //5、请求转发到...页面
        req.getRequestDispatcher("").forward(req, resp);
    }

    /**
     * 范围筛选成绩：老师在所有成绩范围内筛选
     *
     *@url /GradeServlet?action=screenFail
     *
     * ---需要获取的参数：
     * 1、从session中获取Teacher类
     * 2、从Teacher类中获取编号tid
     * 3、从request中获取课程号cno
     * 4、
     *      * ---返回的参数（保存在request中）：
     *      * 类型：List<StudentGrade>
     *      * 参数名：failExam
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void screenScope(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取session中保存的教师类
        HttpSession session = req.getSession();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        //从教师类中获取教师编号
        String tid = teacher.getTid();

        //2、从request中获取范围参数
        int low = Integer.parseInt(req.getParameter("low"));
        int high = Integer.parseInt(req.getParameter("high"));

        //3、调用gradeService获取成绩
        List<StudentGrade> scopeGrade = gradeService.scopeScreen(tid, low, high);
        sqlSession.close();

        //4、保存studentGrades至request域中
        req.setAttribute("scopeGrade", scopeGrade);

        //5、请求转发到...页面
        req.getRequestDispatcher("").forward(req, resp);
    }


}
